package com.example.security.service.impl;

import com.example.security.entity.SysRole;
import com.example.security.entity.SysUser;
import com.example.security.mapper.SysUserMapper;
import com.example.security.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangss
 * @since 2022-10-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser selectByUserName(String username) {
        return sysUserMapper.selectByUserName(username);
    }

    @Override
    public int insertSysUser(String username, String password) {
        return 0;
    }

    @Override
    public List<SysRole> getRoleByUserId(String username) {
        return sysUserMapper.getRoleByUserId(username);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /**
         * 查找用户
         */
        System.out.println("............................."+username);
        SysUser sysUser = null;
        //SysUser user = sysUserMapper.selectByUserName(username);
        try {
             sysUser = this.selectByUserName(username);
            System.out.println("111111111111111111111111111111111"+sysUser);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        /**
         *查找用户对应角色
         */
        //List<SysRole> authorities = sysUserMapper.getRoleByUserId(username);
        List<SysRole> sysRoles = getRoleByUserId(username);
        System.out.println("222222222222222222"+sysRoles);

        UserDetails userDetails = null;

        /**
         * 封装用户权限
         */
        List<SimpleGrantedAuthority> list = sysRoles.stream().map(
                authority -> new SimpleGrantedAuthority(authority.getAuthority())).collect(Collectors.toList());

        /**
         * 将对应值传入security的User对象
         */
        if(sysUser!=null){
            System.out.println(sysUser.getUsername()+"  "+sysUser.getPassword());
            String userA = sysUser.getUsername();
            String pasA = sysUser.getPassword();
            try {
                userDetails =
                        new org.springframework.security.core.userdetails.User(userA, pasA, list);
            }catch (Exception e){
                System.out.println("error:"+e.getMessage());
            }
            return userDetails;
        } else {

            /**
             * 不存在用户抛出为空
             */
            return null;
        }
    }
}
